<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>学生信息更新</title>
    <link rel="stylesheet" th:href="@{/css/plugins/layui/css/layui.css}">
    <!-- 借助layui来帮忙改善页面美观 -->
</head>
<body>
<!-- th:field="*{id}" 自动生成name value属性 -->
<div th:object="${student}" class="layui-form">
    <input type="hidden" id="sid" th:field="*{sid}">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">学号</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="studentNum" th:field="*{studentNum}" class="layui-input" required>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="studentName" th:field="*{studentName}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-inline layui-input-wrap">
                <select id="gender" name="gender" th:field="*{gender}">
                    <option value="">请选择</option>
                    <option value="0" th:selected="${student.gender == '0'}">男</option>
                    <option value="1" th:selected="${student.gender == '1'}">女</option>
                    <option value="2" th:selected="${student.gender == '2'}">保密</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="number" id="age" th:field="*{age}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">出生日期</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="birthday" th:field="*{birthday}" class="layui-input" required>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">班级编号</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="classId" th:field="*{classId}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">宿舍编号</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="hotelId" th:field="*{hotelId}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">电子邮件</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="email" id="email" th:field="*{email}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="phone" th:field="*{phone}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">家庭住址</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="homeAddress" th:field="*{homeAddress}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">紧急联系人姓名</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="emergencyContact" th:field="*{emergencyContact}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">紧急联系人联系电话</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="emergencyContactPhone" th:field="*{emergencyContactPhone}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">专业</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="major" th:field="*{major}" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">学生状态</label>
            <div class="layui-input-inline layui-input-wrap">
                <select id="status" name="status" th:field="*{status}">
                    <option value="">请选择</option>
                    <option value="0" th:selected="${student.status == '0'}">在读</option>
                    <option value="1" th:selected="${student.status == '1'}">毕业</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline layui-input-wrap">
                <textarea id="remark" th:field="*{remark}" class="layui-textarea"></textarea>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</div>

<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/plugins/layui/layui.js}"></script>
<script>
    // 异步请求加载部门列表
    layui.use(function () {
        var laydate = layui.laydate;
        var form = layui.form;
        var $ = layui.$;

        laydate.render({
            elem: '#birthday',
            type: 'date'
        });

        form.on('submit(formDemo)', function (data) {
            $.ajax({
                url: '/student/updateSave',
                type: 'post',
                data: data.field,
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0) {
                        layer.msg('编辑成功', {icon: 1});
                        setTimeout(function () {
                            window.location.href = '/student/list'; // 跳转到学生列表页面
                        }, 1000);
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function () {
                    layer.msg('系统错误', {icon: 2});
                }
            });
            return false; // 阻止表单默认提交
        });
    });
</script>
</body>
</html>
